package cn.hk.user.service.impl;

import cn.hk.common.utils.TimeStampUtil;
import cn.hk.user.domain.UserFamily;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.domain.vo.FamilyPersonVo;
import cn.hk.user.mapper.UserFamilyMapper;
import cn.hk.user.service.UserFamilyService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * app用户家庭组表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-07
 */
@Service
public class UserFamilyServiceImpl extends ServiceImpl<UserFamilyMapper, UserFamily> implements UserFamilyService {
    @Autowired
    private UserFamilyMapper userFamilyMapper;
    /**
     * 添加家庭成员
     * @param familyPersonVo
     * @param userInfo
     */
    @Override
    public void addUserFamily(FamilyPersonVo familyPersonVo, UserInfo userInfo) {
        UserFamily useMaster = userFamilyMapper.selectUserFamilyByUserId(familyPersonVo.getFromUserId());
        UserFamily userFamilyNew = null;
        if (BeanUtil.isEmpty(useMaster)){
            //为空，则新增家庭组code
            userFamilyNew = new UserFamily();
            userFamilyNew.setGroupCode(TimeStampUtil.getTimeStampString());
            userFamilyNew.setAppUserId(familyPersonVo.getFromUserId());
            userFamilyNew.setCreateTime(DateUtil.date());
            userFamilyMapper.insertOne(userFamilyNew);
        }else {
            //不新增家庭组
            //判断家庭组内是否已存在该成员
            if (userFamilyMapper.selectUserFamilyByUserId(userInfo.getId())==null){
                //不存在 则新增家庭成员
                userFamilyNew = new UserFamily();
                userFamilyNew.setGroupCode(useMaster.getGroupCode());
                userFamilyNew.setAppUserId(userInfo.getId());
                userFamilyNew.setCreateTime(DateUtil.date());
                userFamilyMapper.insertOne(userFamilyNew);
            }

        }
    }


}
